using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using OrderApi.Models;

namespace OrderApi.Controllers
{
    [ApiController]
    [Route("api/order")]
    public class OrderController : ControllerBase
    {

        private readonly OrderContext orderDb;

        //构造函数把OrerContext 作为参数，Asp.net core 框架可以自动注入OrderContext对象
        public OrderController(OrderContext context)
        {
            this.orderDb = context;
        }
        //订单号查找
        [HttpGet("{id}")]
        public ActionResult<Order> GetOrder(string id)
        {
            var order = orderDb.Orders.FirstOrDefault(order => order.Id == id);
            if (order == null)
            {
                return NotFound();
            }
            return order;
        }
        //其他查找
        [HttpGet]
        public ActionResult<List<Order>> GetOrders(string customerName,
        string goodName, double? amout)
        {
            IQueryable<Order> query = orderDb.Orders
            .Include(o => o.Items.Select(i => i.GoodsItem))
                .Include("Customer");
            if (customerName != null)
            {
                query = query.Where(o => o.Customer.Name == customerName);
            }
            if (goodName != null)
            {
                query = query.Where(o => o.Items
                  .Count(i => i.GoodsName == goodName) > 0);
            }
            if (amout != null)
            {
                query = query.Where(o => o.Items
                  .Sum(item => item.GoodsItem.Price * item.Quantity) > amout);
            }
            return query.ToList();
        }
        //添加
        [HttpPost]
        public ActionResult<Order> PostOrder(Order newOrder)
        {
            try
            {
                orderDb.Orders.Add(newOrder);
                orderDb.SaveChanges();
            }
            catch (Exception e)
            {
                string error = e.Message;
                if (e.InnerException != null) error = e.InnerException.Message;
                return BadRequest(error);
            }
            return newOrder;
        }
        //更新
        [HttpPut("{id}")]
        public ActionResult<Order> PutOrder(string id, Order order)
        {
            if (id != order.Id)
            {
                return BadRequest("Id cannot be modified!");
            }
            try
            {
                orderDb.Entry(order).State = EntityState.Modified;
                orderDb.SaveChanges();
            }
            catch (Exception e)
            {
                string error = e.Message;
                if (e.InnerException != null) error = e.InnerException.Message;
                return BadRequest(error);
            }
            return NoContent();
        }
        //删除
        [HttpDelete("{id}")]
        public ActionResult DeleteTodoItem(string id)
        {
            try
            {
                var order = orderDb.Orders.FirstOrDefault(t => t.Id == id);
                if (order != null)
                {
                    orderDb.Remove(order);
                    orderDb.SaveChanges();
                }
            }
            catch (Exception e)
            {
                return BadRequest(e.InnerException.Message);
            }
            return NoContent();
        }

    }
}